﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

using System.Web.Resources;
using Microsoft.Reporting.WebForms;

namespace WebInvoicingsys.system.checkstock
{
    public partial class checkstockbill_print : System.Web.UI.Page
    {
        //传入的参数
        string id, start_date, end_date, csid;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                id = Request.Params["id"].ToString();
                start_date = Request.Params["start_date"].ToString();
                end_date = Request.Params["end_date"].ToString();
                csid = Request.Params["csid"].ToString();
            }
            catch
            {
               return false;
            }

            return true;
        }

        private void Return()
        {
            GetParma();

            string url = String.Format("/system/checkstock/checkstockbill_list.aspx?start_date={0}&end_date={1}&csid={2}", start_date, end_date, csid);

            Response.Redirect(url);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            DataSet dsResult = new DataSet();

            string sql = "", error_msg = "";

            Execute exec = new Execute(Session["connectionString"].ToString());

            //构造SQL语句
            sql = @"select  a.csid, 
                            a.billdate,
                            a.remark,
                            a.warehouseid,
                            (select warehouse.warehousename from warehouse where a.warehouseid=warehouse.warehouseid) warehousename
                        from check_stock a
                        where a.csid='{0}'";

            sql = String.Format(sql, id);

            DataSet dsMaster = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (dsMaster == null)
            {
                return null;
            }

            //复制报表的数据表结构
            WebInvoicingsys.system.checkstock.checkstockbill_print_ds report_ds = new WebInvoicingsys.system.checkstock.checkstockbill_print_ds();

            DataTable dtMaster = report_ds.dtMaster.Clone();

            foreach (DataRow dr in dsMaster.Tables[0].Rows)
            {
                DataRow drnew = dtMaster.NewRow();

                drnew["csid"] = dr["csid"].ToString();
                drnew["billdate"] = dr["billdate"].ToString();
                drnew["remark"] = dr["remark"].ToString();
                drnew["warehouseid"] = dr["warehouseid"].ToString();
                drnew["warehousename"] = dr["warehousename"].ToString();

                dtMaster.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dtMaster);

            //取得明细数据
            sql = @"select a.csid, 
                           a.storageid,
                           c.storagename,
                           c.areaid,
                           c.areaname,
                           a.productcode, 
                           b.productname, 
                           a.qty1,
                           a.qty2
                      from check_stock_detail a, product b, v_storage c
                       where a.productcode=b.productcode
                         and a.storageid=c.storageid
                         and a.csid='{0}'";

            sql = String.Format(sql, id);

            DataSet dsDetail = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (dsDetail == null)
            {
                return null;
            }

            DataTable dtDetail = report_ds.dtDetail.Clone();

            foreach (DataRow dr in dsDetail.Tables[0].Rows)
            {
                DataRow drnew = dtDetail.NewRow();

                drnew["dgvID"] = (dtDetail.Rows.Count + 1).ToString();
                drnew["storageid"] = dr["storageid"].ToString();
                drnew["storagename"] = dr["storagename"].ToString();
                drnew["areaid"] = dr["areaid"].ToString();
                drnew["areaname"] = dr["areaname"].ToString();
                drnew["productcode"] = dr["productcode"].ToString();
                drnew["productname"] = dr["productname"].ToString();
                drnew["qty1"] = Decimal.Parse(dr["qty1"].ToString());
                drnew["qty2"] = Decimal.Parse(dr["qty2"].ToString());

                dtDetail.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dtDetail);

            return dsResult;
        }

        private void Bind()
        {
            if (GetParma() == false)
            {
                Return();
            }

            DataSet ds = GetData();

            if (ds == null)
            {
                Return();
            }

            Microsoft.Reporting.WebForms.ReportDataSource rdsMaster = new ReportDataSource("dtMaster", ds.Tables[0]);
            Microsoft.Reporting.WebForms.ReportDataSource rdsDetail = new ReportDataSource("dtDetail", ds.Tables[1]);

            rvPrint.LocalReport.DataSources.Clear();
            rvPrint.LocalReport.DataSources.Add(rdsMaster);
            rvPrint.LocalReport.DataSources.Add(rdsDetail);
            rvPrint.LocalReport.Refresh();

            rvPrint.ShowPrintButton = true;

            rvPrint.LocalReport.DisplayName = "盘点清单";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string login = Session["login"].ToString();

                    if (login.ToUpper() != "1")
                    {
                        Response.Redirect("/default.aspx", false);
                        return;
                    }
                }
                catch
                {
                    Response.Redirect("/default.aspx", false);
                    return;
                }

                Bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Return();
        }
    }
}